<title>服务调用者</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>项目管理</cite></a>
        <a><cite>服务调用者</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="content-consumer">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="调用者名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="appKey" placeholder="appKey" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-consumer" lay-submit lay-filter="LAY-consumer-search">
                        搜索
                    </button>
                    <button class="layui-btn consumer-btn-common" data-type="add">
                        添加
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-consumer" lay-filter="LAY-consumer"></table>
            <script type="text/html" id="table-system-consumer">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <!--                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i-->
                <!--                        class="layui-icon layui-icon-delete"></i>删除</a>-->
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="grant">
                    <i class="layui-icon layui-icon-auz"></i>接口授权
                </a>
                <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">
                    <i class="layui-icon layui-icon-vercode"></i>密钥
                </a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="download">
                    <i class="layui-icon layui-icon-download-circle"></i>下载文档
                </a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'form', 'setter', 'element'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form
            , element = layui.element
            , setter = layui.setter;

        var apiUrl = setter.apiUrl;
        var token = layui.data(setter.tableName).token;
        console.log(token);

        table.render({
            elem: '#LAY-consumer'
            , url: apiUrl + '/mapi/consumer/list'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': token}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                {field: 'name', width: 180, title: '调用者名称'}
                , {field: 'appKey', width: 180, title: 'appKey'}
                , {field: 'enable', width: 60, title: '状态', templet: '<div>{{d.enable==0?"禁用":"正常"}}</div>'}
                , {
                    field: 'createdAt',
                    title: '创建时间',
                    width: 170,
                    templet: '<div>{{layui.util.toDateString(d.createdAt, "yyyy-MM-dd HH:mm:ss")}}</div>'
                }
                , {title: '操作', minWidth: 300, fixed: 'right', toolbar: '#table-system-consumer'}
            ]]
            , text: {
                none: '未检索到数据'
            }
            , done: function () {
                element.render('progress');
            }
        });

        //监听工具条
        table.on('tool(LAY-consumer)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                admin.popup({
                    title: '编辑调用者'
                    , area: ['400px', '360px']
                    , id: 'LAY-popup-consumer-edit'
                    , success: function (layero, index) {
                        view(this.id).render('project/consumer/edit', data).done(function () {
                            var pid = data.id;
                            //监听提交
                            form.on('submit(LAY-consumer-edit-submit)', function (data) {
                                admin.req({
                                    url: apiUrl + '/mapi/consumer/update/' + pid
                                    , method: 'put'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('修改成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-consumer');
                                                layer.close(index);
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            } else if (obj.event === 'detail') {
                admin.popup({
                    title: '调用者详情'
                    , area: ['650px', '480px']
                    , id: 'LAY-popup-consumer-detail'
                    , success: function (layero, index) {
                        view(this.id).render('project/consumer/detail', data);
                    }
                });
            } else if (obj.event === 'download') {
                window.location.href = apiUrl + "/mapi/consumer/grant/doc/" + data.id + '?token=' + token;
            } else if (obj.event === 'grant') {
                admin.popup({
                    title: '接口授权'
                    , area: ['100%', '100%']
                    , id: 'LAY-popup-consumer-grant'
                    , success: function (layero, index) {
                        view(this.id).render('project/consumer/grant', data).done(function () {
                            var pid = data.id;
                            //监听提交
                            form.on('submit(LAY-consumer-grant-submit)', function (data) {
                                admin.req({
                                    url: apiUrl + '/mapi/consumer/grant/' + pid
                                    , method: 'put'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('授权成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-consumer');
                                                layer.close(index);
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            }
        });

        //事件
        var active = {
            add: function () {
                admin.popup({
                    title: '添加调用者'
                    , area: ['400px', '360px']
                    , id: 'LAY-popup-consumer-add'
                    , success: function (layero, index) {
                        view(this.id).render('project/consumer/add').done(function () {
                            //监听提交
                            form.on('submit(LAY-consumer-add-submit)', function (data) {
                                admin.req({
                                    url: apiUrl + '/mapi/consumer/add'
                                    , method: 'post'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('添加成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-consumer');
                                                layer.close(index);
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            }
        };

        //监听搜索
        form.on('submit(LAY-consumer-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-consumer', {
                where: field
            });
        });

        $('.layui-btn.consumer-btn-common').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        function isEmpty(str) {
            if (typeof str == null || str == "" || str == "undefined") {
                return true;
            } else {
                return false;
            }
        }
    });
</script>